package com.freejava.myblog.controller;

import com.freejava.myblog.annotation.PassToken;
import com.freejava.myblog.annotation.UserLoginToken;
import com.freejava.myblog.pojo.MyArticle;
import com.freejava.myblog.services.ArticleService;
import com.freejava.myblog.tools.ErrorEnum;
import com.freejava.myblog.tools.JsonResultObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1")
@Api("Api for ArticleController")
public class ArticleController {
    @Autowired
    ArticleService articleService;

    @ApiOperation(value = "创建文章", notes = "创建一篇文章")
    @PostMapping("/article")
    @UserLoginToken
    public JsonResultObject add(@ApiParam(name = "MyArticle", value = "MyArticle对象数据", required = true) @Validated
                                @RequestBody MyArticle myArticle, BindingResult bindingResult) {
        try {
            if (bindingResult.hasErrors()) {
                return new JsonResultObject("400", "新发布文章失败！", bindingResult.getFieldError().getDefaultMessage(), "Bad Params", null);
            }
            boolean addResult = articleService.add(myArticle);
            if (addResult) {
                return new JsonResultObject("200", "新发布文章成功！", "", "", null);
            } else {
                ErrorEnum enum1 = ErrorEnum.valueOf(ErrorEnum.class, "BAD_PARAM");
                return new JsonResultObject("200", "新发布文章失败！", enum1.getErrorMsg(), enum1.getErrorCode(), null);
            }
        } catch (Exception e) {
            return new JsonResultObject("400", "新发布文章失败！", e.getMessage(), "Bad Params", null);

        }

    }

    // 获取文章列表 不分页，游客同样可以访问
    @ApiOperation(value = "不分页获取文章列表", notes = "不分页获取文章列表")
    @GetMapping("/articles")
    @PassToken
    public JsonResultObject getAll() {
        List<MyArticle> articles = articleService.findAll();
        JsonResultObject result = new JsonResultObject("200", "get  articles", "", "", articles);

        return result;
    }

    // 分页获取文章列表
    @ApiOperation(value = "分页获取文章列表", notes = "分页获取文章列表数据")
    @GetMapping("/articles/{pageNum}")
    @PassToken
    public JsonResultObject getListByPageNum(@ApiParam(name = "pageNum", value = "页码", required = true)
                                                 @PathVariable int pageNum) {
        List<MyArticle> articles = articleService.getListByPageNum(pageNum);
        JsonResultObject result = new JsonResultObject("200", "get  articles", "", "", articles);

        return result;
    }

    @ApiOperation(value = "根据ID获取文章详情", notes = "根据ID获取文章详情")
    @GetMapping("/article/{id}")
    @PassToken
    public JsonResultObject detail(@ApiParam(name = "id", value = "文章ID", required = true) @PathVariable String id) {
        MyArticle article = articleService.detail(id);
        JsonResultObject result = new JsonResultObject("200", "get  articles", "", "", article);

        return result;
    }

    @ApiOperation(value = "修改单篇文章", notes = "修改单篇文章，会传递ID")
    @PutMapping("/article")
    @UserLoginToken
    public JsonResultObject update(@ApiParam(name = "myArticle", value = "MyArticle数据对象，json格式", required = true) @Validated
                                   @RequestBody MyArticle myArticle, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return new JsonResultObject("400", "修改文章失败！", bindingResult.getFieldError().getDefaultMessage(), "40002", null);
        } else {
            if (myArticle.getId() == null && myArticle.getId().equals("")) {
                return new JsonResultObject("400", "修改文章失败！", "no Id", "40003", null);
            } else {
                long unixTime = System.currentTimeMillis() / 1000L;
                int nowUnixTime = (int) unixTime;
                myArticle.setModified(nowUnixTime);
                boolean updateResult = articleService.update(myArticle);

                if (updateResult) {
                    return new JsonResultObject("200", "修改文章成功！", "", "", null);
                } else {
                    ErrorEnum enum1 = ErrorEnum.valueOf(ErrorEnum.class, "BAD_PARAM");
                    return new JsonResultObject("200", "修改文章失败！", enum1.getErrorMsg(), enum1.getErrorCode(), null);
                }
            }
        }
    }

    @ApiOperation(value = "根据ID删除文章", notes = "根据ID删除文章")
    @DeleteMapping("/article/{id}")
    @UserLoginToken
    public JsonResultObject delete(@ApiParam(name = "id", value = "文章ID", required = true) @PathVariable String id) {
        boolean deleteResult = articleService.delete(id);
        if (deleteResult) {
            return new JsonResultObject("200", "删除文章成功！", "", "", null);
        } else {
            ErrorEnum enum1 = ErrorEnum.valueOf(ErrorEnum.class, "BAD_PARAM");
            return new JsonResultObject("200", "删除文章失败！", enum1.getErrorMsg(), enum1.getErrorCode(), null);
        }
    }
}
